using System;
using System.Collections;
using System.Collections.Generic;
using Game;
using Models;
using UnityEngine;

namespace Services
{
    public class LoginResult
    {
        public bool Success { get; set; }
        public string Message { get; set; }
        public Models.PlayerModel PlayerData { get; set; }
    }   

    /**
     * 登录服务
     * 
     * 说明：
     * - 登录服务用于管理玩家登录相关的数据
     * - 登录服务是单例的
     */
    public class LoginService : Service
    {
        private Models.PlayerModel _playerModel;
        public override void OnInit()
        {
            base.OnInit();

            // 正式的工程是在这里绑定网络系统

            this._playerModel = ModelManager.GetModel<Models.PlayerModel>();
        }

        private IEnumerator LoginCoroutine(string username, string password, Action<LoginResult> onComplete)
        {
            // 模拟网络延迟
            yield return new WaitForSeconds(1f);

            // 模拟登录成功
            this._playerModel.Name = username;
            this._playerModel.Id = "1234567890";
            this._playerModel.Token = "1234567890";
            this._playerModel.Avatar = "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png";
            this._playerModel.Level = "1";

            Debug.Log("LoginService: 登录成功");

            onComplete?.Invoke(new LoginResult
            {
                Success = true,
                Message = "登录成功",
                PlayerData = this._playerModel
            });
        }

        public void Login(string username, string password, Action<LoginResult> onComplete)
        {
            GameApp.Instance.StartCoroutine(this.LoginCoroutine(username, password, onComplete));
        }
    }
}